<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.utils.http &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-http">
            
  <h1>Source code for django.utils.http</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">base64</span>
<span class="kn">import</span> <span class="nn">calendar</span>
<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">unicodedata</span>
<span class="kn">from</span> <span class="nn">binascii</span> <span class="kn">import</span> <span class="n">Error</span> <span class="k">as</span> <span class="n">BinasciiError</span>
<span class="kn">from</span> <span class="nn">email.utils</span> <span class="kn">import</span> <span class="n">formatdate</span>

<span class="kn">from</span> <span class="nn">django.utils.datastructures</span> <span class="kn">import</span> <span class="n">MultiValueDict</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_bytes</span><span class="p">,</span> <span class="n">force_str</span><span class="p">,</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="kn">import</span> <span class="n">allow_lazy</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves.urllib.parse</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">quote</span><span class="p">,</span> <span class="n">quote_plus</span><span class="p">,</span> <span class="n">unquote</span><span class="p">,</span> <span class="n">unquote_plus</span><span class="p">,</span> <span class="n">urlparse</span><span class="p">,</span>
    <span class="n">urlencode</span> <span class="k">as</span> <span class="n">original_urlencode</span><span class="p">)</span>

<span class="n">ETAG_MATCH</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;(?:W/)?&quot;((?:</span><span class="se">\\</span><span class="s">.|[^&quot;])*)&quot;&#39;</span><span class="p">)</span>

<span class="n">MONTHS</span> <span class="o">=</span> <span class="s">&#39;jan feb mar apr may jun jul aug sep oct nov dec&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
<span class="n">__D</span> <span class="o">=</span> <span class="s">r&#39;(?P&lt;day&gt;\d{2})&#39;</span>
<span class="n">__D2</span> <span class="o">=</span> <span class="s">r&#39;(?P&lt;day&gt;[ \d]\d)&#39;</span>
<span class="n">__M</span> <span class="o">=</span> <span class="s">r&#39;(?P&lt;mon&gt;\w{3})&#39;</span>
<span class="n">__Y</span> <span class="o">=</span> <span class="s">r&#39;(?P&lt;year&gt;\d{4})&#39;</span>
<span class="n">__Y2</span> <span class="o">=</span> <span class="s">r&#39;(?P&lt;year&gt;\d{2})&#39;</span>
<span class="n">__T</span> <span class="o">=</span> <span class="s">r&#39;(?P&lt;hour&gt;\d{2}):(?P&lt;min&gt;\d{2}):(?P&lt;sec&gt;\d{2})&#39;</span>
<span class="n">RFC1123_DATE</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;^\w{3}, </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> GMT$&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">__D</span><span class="p">,</span> <span class="n">__M</span><span class="p">,</span> <span class="n">__Y</span><span class="p">,</span> <span class="n">__T</span><span class="p">))</span>
<span class="n">RFC850_DATE</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;^\w{6,9}, </span><span class="si">%s</span><span class="s">-</span><span class="si">%s</span><span class="s">-</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> GMT$&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">__D</span><span class="p">,</span> <span class="n">__M</span><span class="p">,</span> <span class="n">__Y2</span><span class="p">,</span> <span class="n">__T</span><span class="p">))</span>
<span class="n">ASCTIME_DATE</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;^\w{3} </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">$&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">__M</span><span class="p">,</span> <span class="n">__D2</span><span class="p">,</span> <span class="n">__T</span><span class="p">,</span> <span class="n">__Y</span><span class="p">))</span>


<div class="viewcode-block" id="urlquote"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.urlquote">[docs]</a><span class="k">def</span> <span class="nf">urlquote</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">safe</span><span class="o">=</span><span class="s">&#39;/&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A version of Python&#39;s urllib.quote() function that can operate on unicode</span>
<span class="sd">    strings. The url is first UTF-8 encoded before quoting. The returned string</span>
<span class="sd">    can safely be used as part of an argument to a subsequent iri_to_uri() call</span>
<span class="sd">    without double-quoting occurring.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">quote</span><span class="p">(</span><span class="n">force_str</span><span class="p">(</span><span class="n">url</span><span class="p">),</span> <span class="n">force_str</span><span class="p">(</span><span class="n">safe</span><span class="p">)))</span></div>
<span class="n">urlquote</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">urlquote</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<div class="viewcode-block" id="urlquote_plus"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.urlquote_plus">[docs]</a><span class="k">def</span> <span class="nf">urlquote_plus</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">safe</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A version of Python&#39;s urllib.quote_plus() function that can operate on</span>
<span class="sd">    unicode strings. The url is first UTF-8 encoded before quoting. The</span>
<span class="sd">    returned string can safely be used as part of an argument to a subsequent</span>
<span class="sd">    iri_to_uri() call without double-quoting occurring.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">quote_plus</span><span class="p">(</span><span class="n">force_str</span><span class="p">(</span><span class="n">url</span><span class="p">),</span> <span class="n">force_str</span><span class="p">(</span><span class="n">safe</span><span class="p">)))</span></div>
<span class="n">urlquote_plus</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">urlquote_plus</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">urlunquote</span><span class="p">(</span><span class="n">quoted_url</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A wrapper for Python&#39;s urllib.unquote() function that can operate on</span>
<span class="sd">    the result of django.utils.http.urlquote().</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">unquote</span><span class="p">(</span><span class="n">force_str</span><span class="p">(</span><span class="n">quoted_url</span><span class="p">)))</span>
<span class="n">urlunquote</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">urlunquote</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">urlunquote_plus</span><span class="p">(</span><span class="n">quoted_url</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A wrapper for Python&#39;s urllib.unquote_plus() function that can operate on</span>
<span class="sd">    the result of django.utils.http.urlquote_plus().</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">unquote_plus</span><span class="p">(</span><span class="n">force_str</span><span class="p">(</span><span class="n">quoted_url</span><span class="p">)))</span>
<span class="n">urlunquote_plus</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">urlunquote_plus</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<div class="viewcode-block" id="urlencode"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.urlencode">[docs]</a><span class="k">def</span> <span class="nf">urlencode</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">doseq</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A version of Python&#39;s urllib.urlencode() function that can operate on</span>
<span class="sd">    unicode strings. The parameters are first cast to UTF-8 encoded strings and</span>
<span class="sd">    then encoded as per normal.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">MultiValueDict</span><span class="p">):</span>
        <span class="n">query</span> <span class="o">=</span> <span class="n">query</span><span class="o">.</span><span class="n">lists</span><span class="p">()</span>
    <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="s">&#39;items&#39;</span><span class="p">):</span>
        <span class="n">query</span> <span class="o">=</span> <span class="n">query</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">original_urlencode</span><span class="p">(</span>
        <span class="p">[(</span><span class="n">force_str</span><span class="p">(</span><span class="n">k</span><span class="p">),</span>
         <span class="p">[</span><span class="n">force_str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">v</span><span class="p">]</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">))</span> <span class="k">else</span> <span class="n">force_str</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">query</span><span class="p">],</span>
        <span class="n">doseq</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="cookie_date"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.cookie_date">[docs]</a><span class="k">def</span> <span class="nf">cookie_date</span><span class="p">(</span><span class="n">epoch_seconds</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Formats the time to ensure compatibility with Netscape&#39;s cookie standard.</span>

<span class="sd">    Accepts a floating point number expressed in seconds since the epoch, in</span>
<span class="sd">    UTC - such as that outputted by time.time(). If set to None, defaults to</span>
<span class="sd">    the current time.</span>

<span class="sd">    Outputs a string in the format &#39;Wdy, DD-Mon-YYYY HH:MM:SS GMT&#39;.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">rfcdate</span> <span class="o">=</span> <span class="n">formatdate</span><span class="p">(</span><span class="n">epoch_seconds</span><span class="p">)</span>
    <span class="k">return</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">-</span><span class="si">%s</span><span class="s">-</span><span class="si">%s</span><span class="s"> GMT&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">rfcdate</span><span class="p">[:</span><span class="mi">7</span><span class="p">],</span> <span class="n">rfcdate</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">11</span><span class="p">],</span> <span class="n">rfcdate</span><span class="p">[</span><span class="mi">12</span><span class="p">:</span><span class="mi">25</span><span class="p">])</span>

</div>
<div class="viewcode-block" id="http_date"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.http_date">[docs]</a><span class="k">def</span> <span class="nf">http_date</span><span class="p">(</span><span class="n">epoch_seconds</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Formats the time to match the RFC1123 date format as specified by HTTP</span>
<span class="sd">    RFC2616 section 3.3.1.</span>

<span class="sd">    Accepts a floating point number expressed in seconds since the epoch, in</span>
<span class="sd">    UTC - such as that outputted by time.time(). If set to None, defaults to</span>
<span class="sd">    the current time.</span>

<span class="sd">    Outputs a string in the format &#39;Wdy, DD Mon YYYY HH:MM:SS GMT&#39;.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">formatdate</span><span class="p">(</span><span class="n">epoch_seconds</span><span class="p">,</span> <span class="n">usegmt</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

</div>
<span class="k">def</span> <span class="nf">parse_http_date</span><span class="p">(</span><span class="n">date</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parses a date format as specified by HTTP RFC2616 section 3.3.1.</span>

<span class="sd">    The three formats allowed by the RFC are accepted, even if only the first</span>
<span class="sd">    one is still in widespread use.</span>

<span class="sd">    Returns an integer expressed in seconds since the epoch, in UTC.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># emails.Util.parsedate does the job for RFC1123 dates; unfortunately</span>
    <span class="c"># RFC2616 makes it mandatory to support RFC850 dates too. So we roll</span>
    <span class="c"># our own RFC-compliant parsing.</span>
    <span class="k">for</span> <span class="n">regex</span> <span class="ow">in</span> <span class="n">RFC1123_DATE</span><span class="p">,</span> <span class="n">RFC850_DATE</span><span class="p">,</span> <span class="n">ASCTIME_DATE</span><span class="p">:</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">m</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">break</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%r</span><span class="s"> is not in a valid HTTP date format&quot;</span> <span class="o">%</span> <span class="n">date</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">year</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s">&#39;year&#39;</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">year</span> <span class="o">&lt;</span> <span class="mi">100</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">year</span> <span class="o">&lt;</span> <span class="mi">70</span><span class="p">:</span>
                <span class="n">year</span> <span class="o">+=</span> <span class="mi">2000</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">year</span> <span class="o">+=</span> <span class="mi">1900</span>
        <span class="n">month</span> <span class="o">=</span> <span class="n">MONTHS</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s">&#39;mon&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">day</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s">&#39;day&#39;</span><span class="p">))</span>
        <span class="n">hour</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s">&#39;hour&#39;</span><span class="p">))</span>
        <span class="nb">min</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s">&#39;min&#39;</span><span class="p">))</span>
        <span class="n">sec</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s">&#39;sec&#39;</span><span class="p">))</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">,</span> <span class="n">day</span><span class="p">,</span> <span class="n">hour</span><span class="p">,</span> <span class="nb">min</span><span class="p">,</span> <span class="n">sec</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">calendar</span><span class="o">.</span><span class="n">timegm</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">())</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
        <span class="n">six</span><span class="o">.</span><span class="n">reraise</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%r</span><span class="s"> is not a valid date&quot;</span> <span class="o">%</span> <span class="n">date</span><span class="p">),</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">2</span><span class="p">])</span>


<span class="k">def</span> <span class="nf">parse_http_date_safe</span><span class="p">(</span><span class="n">date</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Same as parse_http_date, but returns None if the input is invalid.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">parse_http_date</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="c"># Base 36 functions: useful for generating compact URLs</span>

<div class="viewcode-block" id="base36_to_int"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.base36_to_int">[docs]</a><span class="k">def</span> <span class="nf">base36_to_int</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Converts a base 36 string to an ``int``. Raises ``ValueError` if the</span>
<span class="sd">    input won&#39;t fit into an int.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># To prevent overconsumption of server resources, reject any</span>
    <span class="c"># base36 string that is long than 13 base36 digits (13 digits</span>
    <span class="c"># is sufficient to base36-encode any 64-bit integer)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">13</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Base36 input too large&quot;</span><span class="p">)</span>
    <span class="n">value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="mi">36</span><span class="p">)</span>
    <span class="c"># ... then do a final check that the value will fit into an int to avoid</span>
    <span class="c"># returning a long (#15067). The long type was removed in Python 3.</span>
    <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span> <span class="ow">and</span> <span class="n">value</span> <span class="o">&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxint</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Base36 input too large&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">value</span>

</div>
<div class="viewcode-block" id="int_to_base36"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.int_to_base36">[docs]</a><span class="k">def</span> <span class="nf">int_to_base36</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Converts an integer to a base36 string</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">digits</span> <span class="o">=</span> <span class="s">&quot;0123456789abcdefghijklmnopqrstuvwxyz&quot;</span>
    <span class="n">factor</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Negative base36 conversion input.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">integer_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;Non-integer base36 conversion input.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxint</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Base36 conversion input too large.&quot;</span><span class="p">)</span>
    <span class="c"># Find starting factor</span>
    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
        <span class="n">factor</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">36</span> <span class="o">**</span> <span class="n">factor</span><span class="p">:</span>
            <span class="n">factor</span> <span class="o">-=</span> <span class="mi">1</span>
            <span class="k">break</span>
    <span class="n">base36</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="c"># Construct base36 representation</span>
    <span class="k">while</span> <span class="n">factor</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">j</span> <span class="o">=</span> <span class="mi">36</span> <span class="o">**</span> <span class="n">factor</span>
        <span class="n">base36</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">digits</span><span class="p">[</span><span class="n">i</span> <span class="o">//</span> <span class="n">j</span><span class="p">])</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">%</span> <span class="n">j</span>
        <span class="n">factor</span> <span class="o">-=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">base36</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="urlsafe_base64_encode"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.urlsafe_base64_encode">[docs]</a><span class="k">def</span> <span class="nf">urlsafe_base64_encode</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Encodes a bytestring in base64 for use in URLs, stripping any trailing</span>
<span class="sd">    equal signs.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">base64</span><span class="o">.</span><span class="n">urlsafe_b64encode</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="n">b</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">=&#39;</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="urlsafe_base64_decode"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.http.urlsafe_base64_decode">[docs]</a><span class="k">def</span> <span class="nf">urlsafe_base64_decode</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decodes a base64 encoded string, adding back any trailing equal signs that</span>
<span class="sd">    might have been stripped.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">force_bytes</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">base64</span><span class="o">.</span><span class="n">urlsafe_b64decode</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">%</span> <span class="mi">4</span><span class="p">,</span> <span class="n">b</span><span class="s">&#39;=&#39;</span><span class="p">))</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">LookupError</span><span class="p">,</span> <span class="n">BinasciiError</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

</div>
<span class="k">def</span> <span class="nf">parse_etags</span><span class="p">(</span><span class="n">etag_str</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parses a string with one or several etags passed in If-None-Match and</span>
<span class="sd">    If-Match headers by the rules in RFC 2616. Returns a list of etags</span>
<span class="sd">    without surrounding double quotes (&quot;) and unescaped from \&lt;CHAR&gt;.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">etags</span> <span class="o">=</span> <span class="n">ETAG_MATCH</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">etag_str</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">etags</span><span class="p">:</span>
        <span class="c"># etag_str has wrong format, treat it as an opaque string then</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">etag_str</span><span class="p">]</span>
    <span class="n">etags</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;ascii&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;unicode_escape&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">etags</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">etags</span>


<span class="k">def</span> <span class="nf">quote_etag</span><span class="p">(</span><span class="n">etag</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Wraps a string in double quotes escaping contents as necessary.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="s">&#39;&quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">etag</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\\</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\\\</span><span class="s">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">&quot;&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">same_origin</span><span class="p">(</span><span class="n">url1</span><span class="p">,</span> <span class="n">url2</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Checks if two URLs are &#39;same-origin&#39;</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">p1</span><span class="p">,</span> <span class="n">p2</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">url1</span><span class="p">),</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">url2</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">p1</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span> <span class="n">p1</span><span class="o">.</span><span class="n">hostname</span><span class="p">,</span> <span class="n">p1</span><span class="o">.</span><span class="n">port</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="n">p2</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span> <span class="n">p2</span><span class="o">.</span><span class="n">hostname</span><span class="p">,</span> <span class="n">p2</span><span class="o">.</span><span class="n">port</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>


<span class="k">def</span> <span class="nf">is_safe_url</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return ``True`` if the url is a safe redirection (i.e. it doesn&#39;t point to</span>
<span class="sd">    a different host and uses a safe scheme).</span>

<span class="sd">    Always returns ``False`` on an empty url.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">url</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">url</span> <span class="o">=</span> <span class="n">url</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">url</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>
    <span class="c"># Chrome treats \ completely as /</span>
    <span class="n">url</span> <span class="o">=</span> <span class="n">url</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\\</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;/&#39;</span><span class="p">)</span>
    <span class="c"># Chrome considers any URL with more than two slashes to be absolute, but</span>
    <span class="c"># urlparse is not so flexible. Treat any url with three slashes as unsafe.</span>
    <span class="k">if</span> <span class="n">url</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;///&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">False</span>
    <span class="n">url_info</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="c"># Forbid URLs like http:///example.com - with a scheme, but without a hostname.</span>
    <span class="c"># In that URL, example.com is not the hostname but, a path component. However,</span>
    <span class="c"># Chrome will still consider example.com to be the hostname, so we must not</span>
    <span class="c"># allow this syntax.</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">url_info</span><span class="o">.</span><span class="n">netloc</span> <span class="ow">and</span> <span class="n">url_info</span><span class="o">.</span><span class="n">scheme</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>
    <span class="c"># Forbid URLs that start with control characters. Some browsers (like</span>
    <span class="c"># Chrome) ignore quite a few control characters at the start of a</span>
    <span class="c"># URL and might consider the URL as scheme relative.</span>
    <span class="k">if</span> <span class="n">unicodedata</span><span class="o">.</span><span class="n">category</span><span class="p">(</span><span class="n">url</span><span class="p">[</span><span class="mi">0</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;C&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>
    <span class="k">return</span> <span class="p">((</span><span class="ow">not</span> <span class="n">url_info</span><span class="o">.</span><span class="n">netloc</span> <span class="ow">or</span> <span class="n">url_info</span><span class="o">.</span><span class="n">netloc</span> <span class="o">==</span> <span class="n">host</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="ow">not</span> <span class="n">url_info</span><span class="o">.</span><span class="n">scheme</span> <span class="ow">or</span> <span class="n">url_info</span><span class="o">.</span><span class="n">scheme</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;http&#39;</span><span class="p">,</span> <span class="s">&#39;https&#39;</span><span class="p">]))</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.utils.http</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>